//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for CreateEditFavorite.
	/// </summary>
	public class CreateFavorite : TemplatedWebControl
	{
		int userID = 0;
		int itemID = 0;
		FavoriteType favoriteType;
		ApplicationType applicationType;
		IText messageText;
		IButton okButton;

		public CreateFavorite()
		{
			CSContext csContext = CSContext.Current;
			userID = csContext.User.UserID;
			itemID = csContext.GetIntFromQueryString("itemID", 0);
			favoriteType = (FavoriteType)csContext.GetIntFromQueryString("favType", -1);
			applicationType = (ApplicationType)csContext.GetIntFromQueryString("appType", 9999);

			if (itemID == 0)
				throw new Exception();
		}

		protected override void AttachChildControls()
		{
			Head.AddTitle(ResourceManager.GetString("ModalTitle_AddFavorite"), Context);

			Favorite favorite = new Favorite();
			favorite.UserID = userID;
			favorite.ItemID = itemID;
			favorite.FavoriteType = favoriteType;
			favorite.ApplicationType = applicationType;

			Favorites.AddFavorite(favorite);

			messageText = FindText("Message");
			if (messageText != null)
				messageText.Text = GenerateConfirmationMessage();

			okButton = FindButton("OK");
			if (okButton != null)
			{
				okButton.Text = ResourceManager.GetString("OK");
				okButton.Click += new EventHandler(okButton_Click);
			}

		}

		private void okButton_Click(object sender, EventArgs e)
		{
			Modal.ClosePage(this.Page);
		}

		private string GenerateConfirmationMessage()
		{
			switch (favoriteType)
			{
				case FavoriteType.Post:
					return ResourceManager.GetString("CreateFavorite_PostMessage");
				case FavoriteType.Section:
					return ResourceManager.GetString("CreateFavorite_SectionMessage");
				case FavoriteType.User:
					return ResourceManager.GetString("CreateFavorite_UserMessage");
				default:
					throw new NotImplementedException(
						string.Format("Unknown FavoriteType: {0}", favoriteType));
			}
		}
	}
}
